from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
from django.views.decorators.csrf import csrf_exempt
from .evaluateTools import *
from .models import Student,Teacher,StudentEvaluateEnvironment,TeacherEvaluateOrganization
from .checkLogin import check_student,check_teacher


@csrf_exempt
def login(request):
	login_error=''
	if request.method=='POST':
		u_id = request.POST['u_id']
		pwd = request.POST['pwd']
		user = Student.objects.filter(student_id=u_id,pwd=pwd)
		if user:
			request.session['u_id']=u_id
			request.session['u_type'] = 'student'
			return HttpResponseRedirect('/evaluate/s_index/')
		else:
			user = Teacher.objects.filter(teacher_id=u_id,pwd=pwd)
			if user:
				request.session['u_id']=u_id
				request.session['u_type'] = 'teacher'
				return HttpResponseRedirect('/evaluate/t_index/')
		login_error='账号密码错误'
	return render(request,'evaluate/login.html',{'login_error':login_error})


"""
	学生评价 start
"""

# 学生评价主页
@check_student
def s_index(request):
	u_id = request.session.get('u_id')
	try:
		had=StudentEvaluateEnvironment.objects.get(student_id=u_id)
		return  render(request,'evaluate/evaluateSuccee.html')
	except:
		v_list = get_hold_s_evaluation(u_id)   #获取学生待评价数据
		student = v_list.pop()
		print(v_list)
		return render(request,'evaluate/sIndex.html',{'v_list':v_list,'student':student})

#学期总评
@csrf_exempt
def s_evaluate_save(request):
	save_p_eva(request)
	return render(request,'evaluate/evaluateSuccee.html')

#评价教师
@csrf_exempt
def s_evaluate_save_ajax(request):
	save_ev_t(request)
	return HttpResponse('提交成功')

"""
	学生评价 end
"""


"""
	教师评价start
"""
#主页
@check_teacher
def t_index(request):
	u_id = request.session.get('u_id')
	try:
		had = TeacherEvaluateOrganization.objects.get(teacher_id=u_id)
		return render(request,'evaluate/evaluateSuccee.html')
	except:
		teacher = Teacher.objects.get(teacher_id=u_id)
		class_list = get_hold_t_evaluation(u_id)
		return render(request,'evaluate/tIndex.html',{'class_list':class_list,'teacher':teacher})

#教师评价班级
@csrf_exempt
def t_evaluate_save_ajax(request):
	save_ev_c(request)
	return HttpResponse('提交成功')


def t_organization_save(request):
	save_ev_o(request)
	return render(request,'evaluate/evaluateSuccee.html')

"""
	教师评价end
"""




